package com.intellagric.service.agritech.impl;

import com.github.pagehelper.PageHelper;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.common.utils.UUIDUtils;
import com.intellagric.dao.agritech.AgtResourceDao;
import com.intellagric.mapper.AgtResourceMapper;
import com.intellagric.mapper.AgtUserStarMapper;
import com.intellagric.pojo.AgtUserStar;
import com.intellagric.pojo.AgtUserStarExample;
import com.intellagric.service.agritech.UserStarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: cwq
 * @Date: 2019/2/28 19:05
 * @Description:
 */
@Service
public class UserStarServiceImpl implements UserStarService {

    @Autowired
    private AgtUserStarMapper agtUserStarMapper;

//    @Autowired
//    private AgtUserStarDao agtUserStarDao;

    @Autowired
    private AgtResourceMapper agtResourceMapper;

    @Autowired
    private AgtResourceDao agtResourceDao;

    @Override
    public ResponseMessage addUserStar(AgtUserStar userStar) {
        AgtUserStarExample agtUserStarExample = new AgtUserStarExample();
        agtUserStarExample.createCriteria().andIdIsNotNull().andUserIdEqualTo(userStar.getUserId()).andResourceIdEqualTo(userStar.getResourceId());
        if (agtUserStarMapper.selectByExample(agtUserStarExample).size() != 0) {
            return this.cancelUserStar(userStar);
        }
        Date createdTime = new Date();
        String userStarId = UUIDUtils.getID();

        userStar.setId(userStarId);
        userStar.setCreatedTime(createdTime);

        return agtResourceDao.increaseResourceStarCount(userStar.getResourceId(), 1) == 1 ? (
                agtUserStarMapper.insertSelective(userStar) == 1 ? ResponseMessage.success().add("add", userStar) : ResponseMessage.fail()
        ) : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage cancelUserStar(AgtUserStar userStar) {

        userStar.setIsCanceled("1");

        return agtResourceDao.increaseResourceStarCount(userStar.getResourceId(), -1) == 1 ? (
                agtUserStarMapper.updateByPrimaryKeySelective(userStar) == 1 ? ResponseMessage.success() : ResponseMessage.fail()
        ) : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage deleteUserStar(String id) {
        return agtUserStarMapper.deleteByPrimaryKey(id) == 1 ? ResponseMessage.success() : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage editUserStar(AgtUserStar userStar) {
        return agtUserStarMapper.updateByPrimaryKeySelective(userStar) == 1 ? ResponseMessage.success().add("update", userStar) : ResponseMessage.fail();
    }

    @Override
    public AgtUserStar getUserStarById(String id) {
        return agtUserStarMapper.selectByPrimaryKey(id);
    }

    @Override
    public LayuiDataGridResult getUserStarList() {
        AgtUserStarExample agtUserStarExample = new AgtUserStarExample();
        agtUserStarExample.setOrderByClause("created_time ASC");
        agtUserStarExample.createCriteria().andIdIsNotNull();
        List<AgtUserStar> agtUserStarList = agtUserStarMapper.selectByExample(agtUserStarExample);
        return LayuiDataGridResult.success().add(agtUserStarList, agtUserStarList.size());
    }

    @Override
    public LayuiDataGridResult getUserStarByPage(int page, int limit) {
        PageHelper.startPage(page, limit);
        AgtUserStarExample userStarExample = new AgtUserStarExample();
        userStarExample.createCriteria().andIdIsNotNull();
        List<AgtUserStar> userStarList = agtUserStarMapper.selectByExample(userStarExample);
        return LayuiDataGridResult.success().add(userStarList, this.getUserStarList().getCount());
    }

/*    @Override
    public LayuiDataGridResult getUserStarListByVo(AgtUserStarVo agtUserStarVo, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<AgtUserStar> userStarList = agtUserStarDao.selectUserStarListByVo(agtUserStarVo);
        return LayuiDataGridResult.success().add(userStarList, userStarList.size());
    }*/
}
